package pong.mathematic;

public class Rectangle {

	// p1 ===== -+
	// || ----- ||
	// +- ===== p2

	private final Vector2D p1 = new Vector2D(); // up left
	private final Vector2D p2 = new Vector2D(); // down right

	public Rectangle() {
		this(0, 0, 0, 0);
	}

	/**
	 * Rectangle
	 * 
	 * @param p1
	 *            - position left/up edge
	 * @param p2
	 *            - position down/right edge
	 */
	public Rectangle(Vector2D p1, Vector2D p2) {
		this(p1.getX(), p1.getY(), p2.getX(), p2.getY());
	}

	/**
	 * Rectangle
	 * 
	 * @param x1
	 *            - x position left/up edge
	 * @param y1
	 *            - y position left/up edge
	 * @param x2
	 *            - x position down/right edge
	 * @param y2
	 *            - y position down/right edge
	 */
	public Rectangle(float x1, float y1, float x2, float y2) {
		p1.setX(x1);
		p1.setY(y1);

		p2.setX(x2);
		p2.setY(y2);
	}

	public void addCircleToZone(float x, float y, float radius) {
		float up = y + radius;
		float down = y - radius;
		float left = x - radius;
		float right = x + radius;

		if (p1.getX() > left) {
			p1.setX(left);
		}
		if (p1.getY() < up) {
			p1.setY(up);
		}
		if (p2.getX() < right) {
			p2.setX(right);
		}
		if (p2.getY() > down) {
			p2.setY(down);
		}
	}

	public void setNull() {
		p1.setX(0);
		p1.setY(0);

		p2.setX(0);
		p2.setY(0);
	}

	public void addScale(float value) {
		p1.setX(p1.getX() - value);
		p1.setY(p1.getY() + value);

		p2.setX(p2.getX() + value);
		p2.setY(p2.getY() - value);
	}

	public void minusScale(float value) {
		p1.setX(p1.getX() + value);
		p1.setY(p1.getY() - value);

		p2.setX(p2.getX() - value);
		p2.setY(p2.getY() + value);
	}

	public boolean isNull() {
		return p1.getX() == 0f && p1.getY() == 0f && p2.getX() == 0f && p2.getY() == 0f;
	}

	public Vector2D getUpLeft() {
		return p1;
	}

	public Vector2D getDownRight() {
		return p2;
	}

	public Vector2D getUpRight() {
		return new Vector2D(p2.getX(), p1.getY());
	}

	public Vector2D getDownLeft() {
		return new Vector2D(p1.getX(), p2.getY());
	}

	public void setTop(float value) {
		p1.setY(value);
	}

	public void setBottom(float value) {
		p2.setY(value);
	}

	public void setRight(float value) {
		p2.setX(value);
	}

	public void setLeft(float value) {
		p1.setX(value);
	}

	public float getTop() {
		return p1.getY();
	}

	public float getBottom() {
		return p2.getY();
	}

	public float getLeft() {
		return p1.getX();
	}

	public float getRight() {
		return p2.getX();
	}

	public void setRectangle(Rectangle rectangle) {
		setTop(rectangle.getTop());
		setBottom(rectangle.getBottom());
		setLeft(rectangle.getLeft());
		setRight(rectangle.getRight());
	}

}
